<?php

class Admin_SubscriberController extends Zend_Controller_Action
{
    private $_config = null;
    public function init()
    {
        $this->_config = Utils_Global::getConfig('admin', 'upload');
        $this->view->subscriberlzz = 'active';
    }
    
    public function listAction() {
        $page = intval(Utils_Global::$params['page']);
        $limit = intval(Utils_Global::$params['limit']);
        $sortBy = Utils_Global::$params['sort_by'];
        $sortDir = Utils_Global::$params['sort_dir'];
        $message = Utils_Global::$params['errMessage'];
        if($limit <= 0) {
            $limit = 10;
        }
        if($page <= 0) {
            $page = 1;
        }

        $model = Admin_Model_Subscriber::factory();
        $options = array('order' => $sortBy, 'by' => $sortDir?'DESC':'ASC',
                            'offset' => ($page - 1) * $limit, 'limit' => $limit);
        $subscribers = $model->select($options);
        $sortByCol = 'sortBy'.$sortBy;
        $this->view->{$sortByCol} = 1;
        $this->view->sortDir = $sortDir;
        if($sortDir) {
            $this->view->clzzSort = 'icon-chevron-down';
        } else {
            $this->view->clzzSort = 'icon-chevron-up';
        }
        
        $options['sort_by'] = $sortBy;
        $options['sort_dir'] = $sortDir;
        $this->view->params = $options;
        $this->view->subscribers = $subscribers;
        $this->view->title = "Đăng ký theo dõi";
        $this->view->errMessage = $message;
        $this->view->page = $page;
        $this->view->numRowPerPage = $limit;
        $this->view->totalItem = $model->count($options);
        $this->view->currentUrl = $this->view->serverUrl() . $this->view->url(array()) . '?' . http_build_query($options);
    }
    
    public function copyAction() {
        $model = Admin_Model_Subscriber::factory();
        $subscribers = $model->select();
        $this->view->subscribers = $subscribers;
        $this->view->title = "Copy danh sách";
    }
    
    /**
     * Send mail to the mailing list
     * */
    public function sendMailAction() {
        $id = Utils_Global::$params['id'];
        $errMessage = Utils_Global::$params['errMessage'];
        if($id) {
            $model = Admin_Model_DAO_Subscriber::factory();
            $newsLetter = $model->selectNewsLetter(array('id' => $id));
            $this->view->newsletter = $newsLetter[0];
            $this->view->id = $newsLetter[0]['id'];
        } else {
            
        }
        $this->view->errMessage = $errMessage;
        $this->view->title = "Gửi mail cho danh sách đăng ký";
    }
    
    public function saveNewsletterAction() {
        $id = Utils_Global::$params['id'];
        $sent = Utils_Global::$params['sent'];
        $subject = trim(Utils_Global::$params['name']);
        $content = trim(Utils_Global::$params['content']);
        if(!$subject) {
        	Utils_Global::$params['errMessage'] = 'Vui lòng nhập tiêu đề!';
        	$this->_forward('send-mail', 'subscriber', 'admin');
        	return;
        }
        if(!$content) {
        	Utils_Global::$params['errMessage'] = 'Vui lòng nhập nội dung cho newsletter!';
        	$this->_forward('send-mail', 'subscriber', 'admin');
        	return;
        }
        
        $model = Admin_Model_Subscriber::factory();
        $result = 0;
        $mailingList = Utils_Global::getConfig('cms', 'application', 'mailinglist');
        if($id) {
            $data = array('name' => $subject, 'content' => $content, 'last_update' => time());
            $result = $model->updateNewsLetter($id, $data);
            if($sent && $result) {
                Utils_CommonFunction::sendMailList($id);
            }
        } else {
            $data = array('name' => $subject, 'content' => $content, 'dateline' => time(), 'last_update' => time());
            $result = $model->insertNewsLetter($data);
            if($sent && $result) {
                Utils_CommonFunction::sendMailList($result);
            }
        }
        
        if($result) {
            $params = array('errMessage' => "Thay đổi thành công");
            $this->_helper->redirector('list-newsletter', 'subscriber', 'admin', $params);
        } else {
            Utils_Global::$params['errMessage'] = 'Có lỗi xảy ra vui lòng thử lại!';
            $this->_forward('send-mail', 'subscriber', 'admin');
            return;
        }
    }
    
    public function listNewsletterAction() {
        $page = intval(Utils_Global::$params['page']);
        $limit = intval(Utils_Global::$params['limit']);
        $sortBy = Utils_Global::$params['sort_by'];
        $sortDir = Utils_Global::$params['sort_dir'];
        $message = Utils_Global::$params['errMessage'];
        if($limit <= 0) {
        	$limit = 10;
        }
        if($page <= 0) {
        	$page = 1;
        }
        
        $model = Admin_Model_Subscriber::factory();
        $options = array('order' => $sortBy, 'by' => $sortDir?'DESC':'ASC',
        		'offset' => ($page - 1) * $limit, 'limit' => $limit);
        $items = $model->selectNewsLetter($options);
        $sortByCol = 'sortBy'.$sortBy;
        $this->view->{$sortByCol} = 1;
        $this->view->sortDir = $sortDir;
        if($sortDir) {
        	$this->view->clzzSort = 'icon-chevron-down';
        } else {
        	$this->view->clzzSort = 'icon-chevron-up';
        }
        
        $options['sort_by'] = $sortBy;
        $options['sort_dir'] = $sortDir;
        $this->view->params = $options;
        $this->view->items = $items;
        $this->view->title = "Danh sách newsletter";
        $this->view->errMessage = $message;
        $this->view->page = $page;
        $this->view->numRowPerPage = $limit;
        $this->view->totalItem = $model->countNewsLetter($options);
        $this->view->currentUrl = $this->view->serverUrl() . $this->view->url(array()) . '?' . http_build_query($options);
    }
    
    public function deleteNewsletterAction() {
    	$id = intval(Utils_Global::$params['id']);
    	$model = Admin_Model_Subscriber::factory();
    	$result = $model->deleteNewsLetter($id);
    	if($result) {
    		$errMessage = "Xóa thành công";
    	} else {
    		$errMessage = "Xóa không thành công, vui lòng thử lại!";
    	}
    	$this->_redirect('/admin/subscriber/list-newsletter/?errMessage=' . $errMessage);
    }
    
    public function unsubscribeAction() {
        $id = Utils_Global::$params['id'];
        $active = Utils_Global::$params['active'];
        $model = Admin_Model_Subscriber::factory();
        $result = $model->update($id, array('active' => $active, 'last_update' => time()));
        if($result) {
            $params = array('errMessage' => "Thay đổi thành công");
        } else {
            $params = array('errMessage' => "Có lỗi xảy ra vui lòng thử lại!");
        }
        $this->_helper->redirector('list', 'subscriber', 'admin', $params);
    }
}

